package com.toutiao.score


import scala.io.Source

import scala.collection.mutable.{HashMap => HMap}

import scala.math.log

// 关键词权重

object Measure {
  
  def get(location:String) = {
    val source = Source.fromFile(location,"UTF-8")
    val lineIterator = source.getLines
    val xs = lineIterator.map(line => {
      val vs = line.trim.split(" ")
      val v = vs(0)
      val g = vs(1).toInt
      (v,g)
    }).toMap
    
    xs
  }

}

class Scorer(val M:Map[String,Int], val N:Int) {
  
  def score(vs:Array[String]) = {
    val vm = new HMap[String,Int]
    for (v <- vs){
      val c = vm.getOrElse(v, 0)
      vm(v) = c+1
    }
    val s = vm.map(v => {
      val ss =
        if (v._1.size == 1){
          1.1
        }
        else if (M.contains(v._1)){
          v._2*log(N/M(v._1))
        }
        else {
          v._2*log(N/100)
        }
      (v._1,ss)
    })
    s.toMap
  }
  
}


